//
//  PreferencesViewController.swift
//  MyColorPicker
//
//  Created by 火星人 on 2018/9/25.
//  Copyright © 2018年 火星人. All rights reserved.
//

import Cocoa

class PreferencesViewController: NSViewController {
    
    @IBOutlet weak var closeButton: NSButton!
    @IBOutlet weak var popUpButton0: NSPopUpButton!
    @IBOutlet weak var quitButton: NSButton!
    @IBOutlet weak var key0: NSTextField!
    @IBOutlet weak var hideWindowButton: NSButton!
    
//    状态栏图标
    @IBOutlet weak var statusIconButton: NSButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if userDefaults.string(forKey: "showStatusBarIcon") == "showStatusBarIcon" {
            statusIconButton.state = .on
        }
        else {
            statusIconButton.state = .off
        }
        if userDefaults.string(forKey: "closeQuit") == "closeQuit" {
            self.quitButton.state = .on
        }
        else {
            self.quitButton.state = .off
        }
        
        if userDefaults.string(forKey: "hideWindowWhenPickColor") == "hideWindowWhenPickColor" {
            self.hideWindowButton.state = .on
        }
        else {
            self.hideWindowButton.state = .off
        }
        
        var t = "--"
        if let tt = userDefaults.string(forKey: "showMainWindow") {
            t = tt
        }
        self.popUpButton0.removeAllItems()
        self.popUpButton0.addItem(withTitle: "--")
        for i in 0...25 {
            let title = String(UnicodeScalar(UInt8(65 + i)))
            self.popUpButton0.addItem(withTitle: title)
            if title == t {
                self.popUpButton0.selectItem(at: i + 1)
            }
        }
        
        self.popUpButton0.target = self
        self.popUpButton0.action = #selector(self.popupButtonAction)
        
        self.closeButton.frame = CGRect(x: -200, y: -200, width: 100, height: 100)
    }
    
    @objc private func popupButtonAction(){
        
        if let title = self.popUpButton0.titleOfSelectedItem  {
            userDefaults.set(title, forKey: "showMainWindow")
            let ad = NSApp.delegate as? AppDelegate
            ad?.updateHotKey()
        }
        
    }
    
    @IBAction func showStatusAction(_ sender: NSButton) {
        if sender.state == .on {
            userDefaults.set("showStatusBarIcon", forKey: "showStatusBarIcon")
        }
        else{
            userDefaults.removeObject(forKey: "showStatusBarIcon")
        }
        updateStatusIcon()
    }
    
    private func updateStatusIcon(){
        let ad = NSApp.delegate as? AppDelegate
        if userDefaults.string(forKey: "showStatusBarIcon") == "showStatusBarIcon" {
            ad?.showStatusIcon()
        }
        else {
            ad?.hideStatusIcon()
        }
    }
    
    @IBAction func close(_ sender: Any) {
        self.view.window?.close()
    }
    
    @IBAction func closeOrQuitAction(_ sender: NSButton) {
        if sender.state == .on {
            userDefaults.set("closeQuit", forKey: "closeQuit")
        }
        else {
            userDefaults.removeObject(forKey: "closeQuit")
        }
    }
    
    @IBAction func hideWindowButtonAction(_ sender: NSButton) {
        if sender.state == .on {
            userDefaults.set("hideWindowWhenPickColor", forKey: "hideWindowWhenPickColor")
        }
        else {
            userDefaults.removeObject(forKey: "hideWindowWhenPickColor")
        }
    }
    
    
}
